package com.newasia.user.controller;

import com.newasia.commons.result.ResponseData;
import com.newasia.commons.result.ResponseUtil;
import com.newasia.user.IKaptchaService;
import com.newasia.user.ISysRoleService;
import com.newasia.user.ISysUserService;
import com.newasia.user.Shiro.SHA256Util;
import com.newasia.user.Shiro.ShiroUtils;
import com.newasia.user.annotation.Anoymous;
import com.newasia.user.constants.SysRetCodeConstants;
import com.newasia.user.dto.*;
import com.newasia.user.form.UserForm;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 腾讯课堂搜索【咕泡学院】
 * 官网：www.gupaoedu.com
 * 风骚的Mic 老师
 * create-date: 2019/7/23-12:43
 */
@RestController
@RequestMapping("/user")
public class RegisterController {

    @Reference(timeout = 3000)
    IKaptchaService kaptchaService;

    @Reference(timeout = 3000)
    ISysRoleService sysRoleService;

    @Reference(timeout = 3000)
    ISysUserService sysUserService;

    /*@Anoymous
    @PostMapping("/registerTest")
    public ResponseData register(@RequestBody Map<String,String> map, HttpServletRequest request){
        String userName=map.get("userName");
        String userPwd=map.get("userPwd");
        String captcha=map.get("captcha");

        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse response = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }

        UserRegisterRequest registerRequest=new UserRegisterRequest();
        registerRequest.setUserName(userName);
        registerRequest.setUserPwd(userPwd);

        UserRegisterResponse registerResponse=iUserRegisterService.register(registerRequest);

        if(registerResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(null);
        }
        return new ResponseUtil().setErrorMsg(registerResponse.getMsg());
    }*/

    @Anoymous
    @PostMapping("/register")
    public ResponseData userRegister(@RequestBody UserForm form){

        String salt = new SecureRandomNumberGenerator().nextBytes().toHex(); //生成盐值
        String password = SHA256Util.sha256(form.getUserPwd(),salt);

        /*KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse response = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }*/

        SysUserRegisterRequest request = new SysUserRegisterRequest();
        request.setUserName(form.getUserName());
        request.setUserPwd(password);
        request.setSalt(salt);
        request.setCompanyName(form.getCompanyName());
        request.setEmail(form.getEmail());
        request.setMobile(form.getMobile());
        request.setFile(form.getFile());
        request.setRoleId(Long.valueOf(2));
        request.setInvitedCode(form.getInvitedCode());

        SysUserRegisterResponse response = sysUserService.register(request);
        if(response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @Anoymous
    @PostMapping("/updates")
    public ResponseData updates(@RequestBody UserForm form, HttpServletRequest request) {
        String salt = new SecureRandomNumberGenerator().nextBytes().toHex(); //生成盐值
        String password = SHA256Util.sha256(form.getUserPwd(),salt);

        /*KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse response = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }*/

        SysUserRegisterRequest registerRequest = new SysUserRegisterRequest();
        registerRequest.setUserName(form.getUserName());
        registerRequest.setUserPwd(password);
        registerRequest.setSalt(salt);
        registerRequest.setCompanyName(form.getCompanyName());
        registerRequest.setEmail(form.getEmail());
        registerRequest.setMobile(form.getMobile());
        registerRequest.setFile(form.getFile());

        SysUserRegisterResponse registerResponse = sysUserService.updateUser(registerRequest);

        if(registerResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(registerResponse);
        }
        return new ResponseUtil().setErrorMsg(registerResponse);
    }

    @Anoymous
    @PostMapping("/joinDealer")
    public ResponseData joinDealer(@RequestBody UserForm form) {

        JoinDealerRequest request = new JoinDealerRequest();
        request.setInvitedCode(form.getInvitedCode());
        request.setUserId(ShiroUtils.getUserInfo().getUserId());

        JoinDealerResponse response = sysUserService.joinDealer(request);

        if(response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }
}
